package kr.co.doriclassic.chapter23;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class CarGame extends JFrame{
	private JLabel label1;
	private JLabel label2;
	private JLabel label3;
	int x1=100;
	int x2=100;
	int x3=100;
	
	class MyThread implements Runnable{
		@Override
		public void run() {
			for (int i = 0; i < 120; i++) {
				try {Thread.sleep(1000);} catch (InterruptedException e) {}
			x1 += Math.random()*100;
			label1.setBounds(x1, 0, 100, 50);
			x2 += Math.random()*100;
			label2.setBounds(x2, 50, 100, 50);
			x3 += Math.random()*100;
			label3.setBounds(x3, 100, 100, 50);
			}
		}
	}

	public CarGame(){
		setTitle("Car Race");
		setSize(600, 200);
		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setLayout(null);
		label1 = new JLabel();
		label1.setIcon(new ImageIcon("car1.png"));
		label2 = new JLabel();
		label2.setIcon(new ImageIcon("car2.png"));
		label3 = new JLabel();
		label3.setIcon(new ImageIcon("car3.png"));
		add(label1);
		add(label2);
		add(label3);
		label1.setBounds(100, 0, 100, 50);
		label2.setBounds(100, 50, 100, 50);
		label3.setBounds(100, 100, 100, 50);
		(new Thread(new MyThread())).start();
		setVisible(true);
	}
	
	public static void main(String[] args) {
		CarGame t = new CarGame();
	}
}